package com.midian.controller.sys;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.portlet.ModelAndView;

import com.aliyun.common.comm.ServiceClient.Request;
import com.midian.common.exception.BizException;
import com.midian.common.filter.annotation.Validate;
import com.midian.common.system.ErrorConstants;
import com.midian.common.system.RetManager;
import com.midian.common.utils.ContentJSONObject;
import com.midian.model.sys.SysConf;
import com.midian.service.sys.ISysConfService;

import net.sf.json.JSONObject;


/**
 * 系统配置控制器
 *
 */
@Controller("sysConfController")
public class SysConfController {
	
	private static final Logger LOG = Logger.getLogger(SysConfController.class);
	
	/**系统配置业务*/
	private ISysConfService sysConfService;

	@Resource(name="sysConfService")
	public void setSysConfService(ISysConfService sysConfService) {
		this.sysConfService = sysConfService;
	}
	
	
	/**
	 * 获取系统配置列表
	 * @param client_key 
	 * @return
	 */
	@Validate(retCode = RetManager.common_error)
	@RequestMapping(value = "/sys_conf_list", method = RequestMethod.GET)
	public @ResponseBody Object showSysConfList(String client_key,
			@RequestParam(required=false) String code) {
		JSONObject json = new JSONObject();
		try {
			ContentJSONObject contentJson = new ContentJSONObject();
			List<SysConf> sysConfList = this.sysConfService.showAllSysConfList(code);
			for (SysConf sysConf : sysConfList) {
				if(ISysConfService.SYS_CONF_TYPE_EDITOR.equals(sysConf.getType())){
					//以下路径暂时先写死
					contentJson.put(sysConf.getCode(), "http://120.76.103.214:8080/FutureBabyApp/link_page?code="+sysConf.getCode());
				}else{
					contentJson.put(sysConf.getCode(), sysConf.getVal());
				}
			}
			
//			contentJson.put("about_us_url","http://m.mdkg.net/");
//			contentJson.put("service_term_url","http://m.mdkg.net/");
//			contentJson.put("service_phone","02085167161");
			
			json.put("content", contentJson.getContent());
			
		} catch (Exception e) {
			LOG.error("showSysConfList-->exception");
//			try {
//				throw e;
//			} catch (Exception e1) {
//				e1.printStackTrace();
//			}
		}
		return json;
	}
	
	/**
	 * 获取测试包状态
	 * @param client_key 
	 * @return
	 */
	@Validate
	@RequestMapping(value = "/beta_status", method = RequestMethod.GET)
	public @ResponseBody Object showBetaStatus(String client_key) {
		JSONObject contentJson = new JSONObject();
		contentJson.put("status", "1");//1：可用，0：不可用
		JSONObject json = new JSONObject();
		json.put("content", contentJson);
		return json;
	}
	
	/**
	 * 服务页面（合作流程/关于我们/支付须知）
	 * @return
	 */
//	@RequestMapping(value="/link_page" ,method=RequestMethod.GET)
//	@ResponseBody
//	public  ModelAndView aboutUs(@RequestParam("code") String code){
//		ModelAndView mav = new ModelAndView("/web/serviceTermPage.jsp");
//		try {
//			SysConf sysConf = this.sysConfService.getSysConfByCode(code);
//			mav.addObject("sysConf", sysConf);
////			mav.setView("/web/serviceTermPage.jsp");
//		} catch (BizException e) {
//			e.printStackTrace();
//		}
//		
//		return mav;
//	}
	
	@RequestMapping(value="/link_page",method=RequestMethod.GET)
//	@ResponseBody
	public String aboutUs(@RequestParam("code") String code,Model model ){
		try {
			SysConf sysConf = this.sysConfService.getSysConfByCode(code);
			model.addAttribute("sysConf", sysConf);
		} catch (BizException e) {
			e.printStackTrace();
		}
		
		return "serviceTermPage";
	}
	
	
	
}
